package view;
import events.ResizeEvent;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.filters.ColorMatrixFilter;
import gui.buttons.ImageButton;
import model.LevelEditorModel;
import model.LevelEditorModelEvent;
import model.ModelUpdateType;
import view.events.LevelEditorViewOtherEvent;

enum E_ActionType
{
	ADD_PHYSIC_POINT;
}

/**
 * @author Damien Mabin
 */
class ActionBar extends Sprite
{
	private var m_Style 			: { backgroundColor : UInt, buttonBackgroundColor : UInt, buttonForegroundColor : UInt };
	private var m_ButtonStyle		: { backgroundColor : UInt, backgroundAlpha : Float, borderColor : UInt, borderAlpha : Float, borderSize : Int, foregroundColor : UInt };
	private var m_ButtonSize		: Float;
	
	private var m_DisableFilter		: ColorMatrixFilter;
	private var m_Actions			: Array<{ button:ImageButton, action:E_ActionType, descriptor:String }>;
	private var m_EnabledActions	: Array<E_ActionType>;
	private var m_Model				: LevelEditorModel;
	
	public function new(_Model:LevelEditorModel, ?_Style:{ backgroundColor : UInt, buttonBackgroundColor : UInt, buttonForegroundColor : UInt }) 
	{
		super();
		m_Style = _Style;
		m_Model = _Model;
		if(_Style == null)
		{
			m_Style = { backgroundColor : 0x000000, buttonBackgroundColor : 0xFFFF00, buttonForegroundColor : 0xFFFFFF };
		}
		m_ButtonStyle = { backgroundColor : m_Style.buttonBackgroundColor, backgroundAlpha : 1.0, borderColor : 0x454545, borderAlpha : 1.0, borderSize : 1, foregroundColor : m_Style.buttonForegroundColor };
		m_Actions = new Array<{ button:ImageButton, action:E_ActionType, descriptor:String }>();
		m_EnabledActions = new Array<E_ActionType>();
		m_ButtonSize = 70;
		
		var matrix:Array<Float> = new Array<Float>();
		matrix = matrix.concat([0.5, 0, 0, 0, 0]); // red
		matrix = matrix.concat([0, 0.5, 0, 0, 0]); // green
		matrix = matrix.concat([0, 0, 0.5, 0, 0]); // blue
		matrix = matrix.concat([0, 0, 0, 1.0, 0]); // alpha
		m_DisableFilter = new ColorMatrixFilter(matrix);
		
		// Set all possible actions :
		m_Actions.push( { button:new ImageButton(new AddPhysicPoint(), m_ButtonStyle), action:ADD_PHYSIC_POINT, descriptor:"Add new physic point" } );
		
		for(act in m_Actions)
		{
			act.button.dispatchEvent(new ResizeEvent(m_ButtonSize, m_ButtonSize));
			act.button.m_Image.mouseEnabled = false;
			this.addChild(act.button);
			act.button.mouseEnabled = false;
			act.button.addEventListener(MouseEvent.MOUSE_DOWN, OnMouseEvent);
			act.button.filters = [m_DisableFilter];
		}
		
		m_Model.addEventListener(LevelEditorModelEvent.EVENT, OnModelUpdate);
		this.addEventListener(ResizeEvent.EVENT, OnResizeEvent);
	}
	
	private function OnMouseEvent(_Evt:MouseEvent):Void
	{
		for(act in m_Actions)
		{
			if(act.button == _Evt.currentTarget)
			{
				var isEnabled : Bool = false;
				for(enabledAction in m_EnabledActions)
				{
					if(enabledAction == act.action)
					{
						isEnabled = true;
						break;
					}
				}
				if(isEnabled)
				{
					this.dispatchEvent(new LevelEditorViewOtherEvent(E_OtherInteractionType.CHANGE_PHYSIC_PROPERTY(E_PhysicPropertyType.ADD_PHYSIC_POINT, null)));
				}
			}
		}
	}
	
	private function OnModelUpdate(_Evt:LevelEditorModelEvent)
	{
		switch(_Evt.m_Type)
		{
			case ADD_PHYSIC_POINT_TO_SELECTION(pt) :
			{
				if(m_Model.VUE_GetSelection(phx.Vector).length == 1)
				{
					m_EnabledActions.push(ADD_PHYSIC_POINT);
				}
				else
				{
					m_EnabledActions.remove(ADD_PHYSIC_POINT);
				}
			}
			case REMOVE_PHYSIC_POINT_FROM_SELECTION(pt) :
			{
				if(m_Model.VUE_GetSelection(phx.Vector).length == 1)
				{
					m_EnabledActions.push(ADD_PHYSIC_POINT);
				}
				else
				{
					m_EnabledActions.remove(ADD_PHYSIC_POINT);
				}
			}
			default :
			{
				
			}
		}
		OnResizeEvent(new ResizeEvent(width, height));
	}
	
	private function OnResizeEvent(_Evt:ResizeEvent):Void
	{
		this.graphics.clear();
		this.graphics.beginFill(m_Style.backgroundColor);
		this.graphics.drawRect(0, 0, _Evt.m_Width, _Evt.m_Height);
		this.graphics.endFill();
		
		var margin : Float = 5;
		var verticalOffset : Float = margin;
		var horizontalOffset : Float = margin;
		
		for(act in m_Actions)
		{
			var isEnabled : Bool = false;
			for(enabledAction in m_EnabledActions)
			{
				if(enabledAction == act.action)
				{
					isEnabled = true;
					break;
				}
			}
			if(isEnabled)
			{
				act.button.mouseEnabled = true;
				act.button.filters = [];
			}
			else
			{
				act.button.mouseEnabled = false;
				act.button.filters = [m_DisableFilter];
			}
			act.button.x = horizontalOffset;
			act.button.y = verticalOffset;
			horizontalOffset += m_ButtonSize + margin;
			
			if(horizontalOffset + m_ButtonSize > _Evt.m_Width)
			{
				horizontalOffset = margin;
				verticalOffset += m_ButtonSize + margin;
			}
		}
	}
	
}